package com.example.demo.config;

import com.example.demo.enity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//定义拦截规则


//将类的实例化对象扫描到ioc容器中

//@Component
public class MyIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拿到浏览器的缓存空间
        HttpSession session = request.getSession();
        //寻找对应的键值对  并且 将它向下转型成需要的类型
        User user = (User) session.getAttribute("user");
//        String uri = request.getRequestURI();
        //判断对应的value值是否为空
        if (null == user) {
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
